﻿Imports FinanceiroDAL
Imports System.Text.RegularExpressions
Public Class ContasReceberBLL
    Dim objDAL As ContasReceberDAL
#Region "Propriedades"
    Property ctrcodigo As Integer
    Property vencodigo As Nullable(Of Integer)
    Property ccccodigo As Nullable(Of Integer)
    Property ctrdtemissao As Date
    Property clicodigo As Nullable(Of Integer)
    Property valortotal As Decimal
    Property lCtasreceber As New List(Of itensCtasReceberBLL)
#End Region
#Region "Construtores"
    Sub New()
    End Sub
    Sub New(ByVal ctr_codigo As Integer, ByVal ven_codigo As Nullable(Of Integer), ByVal ccc_codigo As Integer, ByVal dataemissao As Date, ByVal cli_codigo As Integer, ByVal valortot As Decimal)
        Me.ctrcodigo = ctr_codigo
        Me.vencodigo = ven_codigo
        Me.ccccodigo = ccc_codigo
        Me.ctrdtemissao = dataemissao
        Me.clicodigo = cli_codigo
        Me.valortotal = valortot
    End Sub
    Sub New(codigo)
        carrega(codigo, 0)
    End Sub
    Sub New(ByVal cod1 As Integer, ByVal cod2 As Integer)
        carrega(cod1, cod2)
    End Sub
#End Region
#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer, ByVal cod2 As Integer)
        Dim tab As DataTable
        Try
            objDAL = New ContasReceberDAL
            tab = objDAL.localizar(codigo, cod2)
            If (tab.Rows.Count > 0) Then
                Me.ctrcodigo = Integer.Parse(tab.Rows(0)("ctr_codigo").ToString)
                If (tab.Rows(0)("ven_codigo") Is DBNull.Value) Then
                    Me.vencodigo = Nothing
                Else
                    Me.vencodigo = tab.Rows(0)("ven_codigo").ToString
                End If
                If (tab.Rows(0)("ccc_codigo") Is DBNull.Value) Then
                    Me.ccccodigo = Nothing
                Else
                    Me.ccccodigo = tab.Rows(0)("ccc_codigo").ToString
                End If

                Me.ctrdtemissao = tab.Rows(0)("ctr_dtemissao").ToString
                If (tab.Rows(0)("cli_codigo") Is DBNull.Value) Then
                    Me.clicodigo = Nothing
                Else
                    Me.clicodigo = tab.Rows(0)("cli_codigo").ToString
                End If

                Me.valortotal = tab.Rows(0)("ctr_valortotal").ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
    End Sub
#End Region
#Region "Limpar"
    Sub limpar()
        Me.ctrcodigo = Nothing
        Me.vencodigo = Nothing
        Me.ccccodigo = Nothing
        Me.ctrdtemissao = Nothing
        Me.clicodigo = Nothing
        Me.valortotal = Nothing
        lCtasreceber.Clear()
    End Sub
#End Region
#Region "Métodos"
#Region "Inserir"
    Function Inserir() As Integer
        Try
            If (validacampos()) Then
                objDAL = New ContasReceberDAL
                Me.ctrcodigo = objDAL.inserir(Me.ctrcodigo, Me.vencodigo, Me.ccccodigo, Me.ctrdtemissao, Me.clicodigo, Me.valortotal)
                If (Me.ctrcodigo > 0) Then
                    'Dim objitdal As New itensCtasReceberBLL
                    'objitdal.excluir(Me.ctrcodigo)
                    insereItensCtasReceber()
                End If
            Else
                Throw New Exception("Preencher campos obrigatórios!")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return Me.ctrcodigo
    End Function
#End Region
#End Region
#Region "validacampos"
    Function validacampos() As Boolean
        If (Me.clicodigo < 0) Then Throw New Exception("Preencha o campo Cliente")
        Return True
    End Function
#End Region
#Region "Métodos Itens Contas Receber (lista)"
    Sub insereItensCtasReceber()
        Try
            For Each item As itensCtasReceberBLL In lCtasreceber
                item.ctrcodigo = Me.ctrcodigo 'em cr 0 e 57
                item.inserealteraICtasRec()
            Next
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
#End Region
#Region "Localiza dAta"
    Function localizadata(ByVal dataIni As DateTime, ByRef datafim As DateTime) As DataTable 'buscar datas cbfiltro
        Dim ret As DataTable
        Try
            objDAL = New ContasReceberDAL
            If dataIni <= datafim Then
                ret = objDAL.localizadata(dataIni, datafim)
            Else : Throw New Exception("Data inical maior que data final")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
#End Region
#Region "Buscar CBFiltro"
    Function localizar(ByVal descricao As String, tipo As Integer) As DataTable
        Dim ret As DataTable
        Try
            objDAL = New ContasReceberDAL
            ret = objDAL.localizar(descricao, tipo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
#End Region
#Region "Buscar ItensCompra"
    Public Function buscaritens() As DataTable
        Dim tab As New DataTable
        Try
            Dim objitens As New ContasReceberDAL
            tab = objitens.localizar(Me.ctrcodigo, 4)
        Catch ex As Exception
            Throw New Exception("Erro ao localizar lista de itens")
        End Try
        Return tab

    End Function
#End Region
End Class
